package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.merchant.domain.ImsYzzcSunBranchAdmin;
import com.ruoyi.merchant.service.IImsYzzcSunBranchAdminService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 门店管理员信息管理
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/shop/branchadmin")
public class ImsYzzcSunBranchAdminController extends BaseController
{
    @Autowired
    private IImsYzzcSunBranchAdminService imsYzzcSunBranchAdminService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询门店管理员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ImsYzzcSunBranchAdmin imsYzzcSunBranchAdmin)
    {
        startPage();
        List<ImsYzzcSunBranchAdmin> list = imsYzzcSunBranchAdminService.selectImsYzzcSunBranchAdminList(imsYzzcSunBranchAdmin);
        for(ImsYzzcSunBranchAdmin branchAdmin:list){
            if (branchAdmin != null && branchAdmin.getUid() != null) {
                SysUser sysUser = userService.selectUserById(branchAdmin.getUid());
                if (sysUser != null) {
                    branchAdmin.setUname(sysUser.getUserName());
                    branchAdmin.setPhone(sysUser.getPhonenumber());
                }
            }
        }

        List<ImsYzzcSunBranchAdmin> filteredPeople = list.stream() // 将列表转换为Stream
                .filter(person -> StringUtils.isBlank(imsYzzcSunBranchAdmin.getUname()) || person.getUname().contains(imsYzzcSunBranchAdmin.getUname())) // 过滤出名字包含搜索条件的人
                .filter(person -> StringUtils.isBlank(imsYzzcSunBranchAdmin.getPhone()) || person.getPhone().contains(imsYzzcSunBranchAdmin.getPhone())) // 过滤出名字包含搜索条件的人
                .collect(Collectors.toList()); // 收集结果到新列表

        return getDataTable(filteredPeople);
    }

    /**
     * 导出门店管理员列表
     */
    @Log(title = "门店管理员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImsYzzcSunBranchAdmin imsYzzcSunBranchAdmin)
    {
        List<ImsYzzcSunBranchAdmin> list = imsYzzcSunBranchAdminService.selectImsYzzcSunBranchAdminList(imsYzzcSunBranchAdmin);
        ExcelUtil<ImsYzzcSunBranchAdmin> util = new ExcelUtil<ImsYzzcSunBranchAdmin>(ImsYzzcSunBranchAdmin.class);
        util.exportExcel(response, list, "门店管理员数据");
    }

    /**
     * 获取门店管理员详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imsYzzcSunBranchAdminService.selectImsYzzcSunBranchAdminById(id));
    }

    /**
     * 新增门店管理员
     */
    @Log(title = "门店管理员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImsYzzcSunBranchAdmin imsYzzcSunBranchAdmin)
    {
        imsYzzcSunBranchAdmin.setCreatetime(DateUtils.getNowDate());
        return toAjax(imsYzzcSunBranchAdminService.insertImsYzzcSunBranchAdmin(imsYzzcSunBranchAdmin));
    }

    /**
     * 修改门店管理员
     */
    @Log(title = "门店管理员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImsYzzcSunBranchAdmin imsYzzcSunBranchAdmin)
    {
        return toAjax(imsYzzcSunBranchAdminService.updateImsYzzcSunBranchAdmin(imsYzzcSunBranchAdmin));
    }

    /**
     * 删除门店管理员
     */
    @Log(title = "门店管理员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsYzzcSunBranchAdminService.deleteImsYzzcSunBranchAdminByIds(ids));
    }
}
